package com.zr.hospital.controller;

import com.zr.hospital.entity.User;
import com.zr.hospital.service.UserService;
import com.zr.hospital.view.Result;
import com.zr.hospital.view.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: silence
 * @Email: 792245022@qq.com
 * @Date: 2022/3/29 17:23
 * @ClassName: UserController
 * @Package: com.zr.hospital.controller
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@CrossOrigin // 处理跨域
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/select")
    public Result select() {
        return new Result(true, StatusCode.OK, "查询成功", userService.list());
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody User user) {
        userService.save(user);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        // 通过id进行修改，user需要带有id值
        userService.updateById(user);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody User user) {
        // 通过id进行删除，user需要带有id值
        userService.removeById(user);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
